import*as e from"../../../core/host/host.js";import*as t from"../../../core/platform/platform.js";import*as o from"../../legacy/legacy.js";import*as s from"../../lit-html/lit-html.js";import*as i from"../helpers/helpers.js";import*as r from"../render_coordinator/render_coordinator.js";import*as n from"../icon_button/icon_button.js";import*as l from"../../../core/i18n/i18n.js";const a=new CSSStyleSheet;a.replaceSync(':host{height:100%;display:block;position:relative}.wrapping-container{overflow-y:auto;height:100%}.wrapping-container:not(.show-scrollbar)::-webkit-scrollbar{display:none}table{border-spacing:0;width:100%;height:100%;table-layout:fixed}tr{outline:none}thead tr{height:27px}tbody tr{background-color:var(--override-data-grid-row-background-color,--color-background)}tbody tr.selected{background-color:var(--color-background-elevation-1)}td,\nth{padding:1px 4px;border-left:1px solid var(--color-details-hairline);color:var(--color-text-primary);line-height:var(--table-row-height);height:var(--table-row-height);user-select:text;white-space:nowrap;text-overflow:ellipsis;overflow:hidden}th{font-weight:normal;text-align:left;border-bottom:1px solid var(--color-details-hairline);position:sticky;top:0;z-index:2;background-color:var(--color-background-elevation-1)}td:focus,\nth:focus{outline:var(--color-primary-old) auto 1px}th:hover.sortable{background:var(--color-background-elevation-0)}.cell-resize-handle{top:0;height:100%;z-index:3;width:20px;cursor:col-resize;position:absolute}td.firstVisibleColumn,\nth.firstVisibleColumn{border-left:none}.hidden{display:none}.filler-row td{height:100%;pointer-events:none;padding:0}.filler-row.empty-table td{padding:1px}[aria-sort="descending"]::after{content:"";width:0;border-left:0.4em solid transparent;border-right:0.4em solid transparent;border-top:0.4em solid var(--color-text-secondary);position:absolute;right:0.5em;top:0.85em}[aria-sort="ascending"]::after{content:"";width:0;border-bottom:0.4em solid var(--color-text-secondary);border-left:0.4em solid transparent;border-right:0.4em solid transparent;position:absolute;right:0.5em;top:0.7em}@media (forced-colors: active){th.sortable{outline:Highlight auto 1px}th:hover.sortable{background-color:Highlight}[aria-sort="ascending"]::after{forced-color-adjust:none;border-bottom:0.4em solid buttonText}[aria-sort="descending"]::after{forced-color-adjust:none;border-top:0.4em solid buttonText}}\n/*# sourceURL=dataGrid.css */\n');class c extends Event{static eventName="columnheaderclick";data;constructor(e,t){super(c.eventName),this.data={column:e,columnIndex:t}}}class d extends Event{static eventName="contextmenucolumnsortclick";data;constructor(e){super(d.eventName),this.data={column:e}}}class h extends Event{static eventName="contextmenuheaderresetclick";constructor(){super(h.eventName)}}class u extends Event{static eventName="newuserfiltertext";data;constructor(e){super(u.eventName,{composed:!0}),this.data={filterText:e}}}class m extends Event{static eventName="cellfocused";data;constructor(e,t){super(m.eventName,{composed:!0}),this.data={cell:e,row:t}}}var g=Object.freeze({__proto__:null,ColumnHeaderClickEvent:c,ContextMenuColumnSortClickEvent:d,ContextMenuHeaderResetClickEvent:h,NewUserFilterTextEvent:u,BodyCellFocusedEvent:m});function f(e,t){const o=!t.visible,s=e.data.columns.map((e=>(e===t&&(e.visible=o),e)));e.data={...e.data,columns:s}}function p(e,t){const{columns:o}=e.data;for(const s of o)s.hideable&&t.defaultSection().appendCheckboxItem(s.title,(()=>{f(e,s)}),s.visible)}function w(e,t){const o=e.data.columns.filter((e=>!0===e.sortable));if(o.length>0)for(const s of o)t.defaultSection().appendItem(s.title,(()=>{e.dispatchEvent(new d(s))}))}const b=e=>s.html`${e}`;var C=Object.freeze({__proto__:null,primitiveRenderer:b,codeBlockRenderer:e=>{if(!e)return s.nothing;const t=String(e);return s.html`<code>${t}</code>`}});function v(e){return JSON.stringify(e.map((e=>e.value instanceof n.Icon.Icon?null:e.value))).toLowerCase()}function S(e,t){const o=e.cells.find((e=>e.columnId===t));if(void 0===o)throw new Error(`Found a row that was missing an entry for column ${t}.`);return o}function x(e){return e.renderer?e.renderer(e.value):b(e.value)}function R(e,t){const o=e.filter((e=>e.visible)).reduce(((e,t)=>e+t.widthWeighting),0),s=e.find((e=>e.id===t));if(!s)throw new Error(`Could not find column with ID ${t}`);if(s.widthWeighting<1)throw new Error(`Error with column ${t}: width weightings must be >= 1.`);return s.visible?Math.round(s.widthWeighting/o*100):0}function y(e){const{key:o,currentFocusedCell:s,columns:i,rows:r}=e,[n,l]=s;switch(o){case"ArrowLeft":{if(n===i.findIndex((e=>e.visible)))return[n,l];let e=n;for(let t=e-1;t>=0;t--){if(i[t].visible){e=t;break}}return[e,l]}case"ArrowRight":{let e=n;for(let t=e+1;t<i.length;t++){if(i[t].visible){e=t;break}}return[e,l]}case"ArrowUp":{const e=i.some((e=>!0===e.sortable))?0:1;if(l===e)return[n,l];let t=l;for(let o=l-1;o>=e;o--){if(0===o){t=0;break}if(!r[o-1].hidden){t=o;break}}return[n,t]}case"ArrowDown":{if(0===l){const e=r.findIndex((e=>!e.hidden));return e>-1?[n,e+1]:[n,l]}let e=l;for(let t=e+1;t<r.length+1;t++){if(!r[t-1].hidden){e=t;break}}return[n,e]}default:return t.assertNever(o,`Unknown arrow key: ${o}`)}}const M=e=>{const{columns:t,rows:o}=e,s=t.some((e=>!0===e.sortable))?0:o.findIndex((e=>!e.hidden))+1;return[t.findIndex((e=>e.visible)),s]},I=e=>e.length<25?e:e.substr(0,20)+"…";var $=Object.freeze({__proto__:null,getStringifiedCellValues:v,getRowEntryForColumnId:S,renderCellValue:x,calculateColumnWidthPercentageFromWeighting:R,handleArrowKeyNavigation:y,calculateFirstFocusableCell:M,getCellTitleFromCellContent:I});const H=r.RenderCoordinator.RenderCoordinator.instance(),D={sortBy:"Sort By",resetColumns:"Reset Columns",headerOptions:"Header Options",enterToSort:"Column sort state: {PH1}. Press enter to apply sorting filter",sortAsc:"ascending",sortDesc:"descending",sortNone:"none"},O=l.i18n.registerUIStrings("ui/components/data_grid/DataGrid.ts",D),F=l.i18n.getLocalizedString.bind(void 0,O),T=new Set([" ","Enter"]);class k extends HTMLElement{static litTagName=s.literal`devtools-data-grid`;#e=this.attachShadow({mode:"open"});#t=[];#o=[];#s=null;#i=!1;#r="NOT_SCROLLED";#n=void 0;#l=void 0;#a=10;#c=!1;#d=null;#h=new WeakMap;#u=new ResizeObserver((()=>{this.#m()}));#g=this.#f.bind(this);#p=[0,1];#w=null;#b=!1;#C=!1;#v=!1;connectedCallback(){this.#e.adoptedStyleSheets=[a],i.SetCSSProperty.set(this,"--table-row-height","18px")}get data(){return{columns:this.#t,rows:this.#o,activeSort:this.#s,contextMenus:this.#n,label:this.#l,paddingRowsCount:this.#a,showScrollbar:this.#c}}set data(e){if(this.#t=e.columns,this.#o=e.rows,this.#o.forEach(((e,t)=>{this.#h.set(e,t)})),this.#s=e.activeSort,this.#n=e.contextMenus,this.#l=e.label,this.#c=e.showScrollbar,this.#b||(this.#p=M({columns:this.#t,rows:this.#o})),void 0!==e.paddingRowsCount&&(this.#a=e.paddingRowsCount),this.#b&&this.#S()){const[e,t]=this.#x(),o=e>this.#t.length,s=t>this.#o.length;(o||s)&&(this.#w=[o?this.#t.length:e,s?this.#o.length:t])}this.#R()}#y(){return"SCROLLED_TO_BOTTOM"===this.#r||!this.#C&&"MANUAL_SCROLL_NOT_BOTTOM"!==this.#r}#M(){if(!1===this.#b||!this.#y())return;const e=this.#e.querySelector(".wrapping-container");e&&H.scroll((()=>{const t=e.scrollHeight;e.scrollTo(0,t)}))}#I(){this.#b||this.#u.observe(this.#e.host)}#S(){return null!==this.#w}#$(){if(!this.#w)return null;const[e,t]=this.#w;return this.#e.querySelector(`[data-row-index="${t}"][data-col-index="${e}"]`)}#H(e){e.focus()}#D([e,t]){if(this.#C=!0,this.#w&&this.#w[0]===e&&this.#w[1]===t)return;this.#w=[e,t],this.#R();const s=this.#$();if(s&&(this.#H(s),0===t&&this.#t[e].sortable)){const t=this.#O(this.#t[e]);o.ARIAUtils.alert(F(D.enterToSort,{PH1:t||""}))}}#O(e){switch(this.#F(e)){case"ascending":return D.sortAsc;case"descending":return D.sortDesc;case"none":return D.sortNone}}#T(e){const o=e.key;if(!this.#w)return;if(T.has(o)){const[e,t]=this.#w,o=this.#t[e];0===t&&o&&o.sortable&&this.#k(o,e)}if(!t.KeyboardUtilities.keyIsArrowKey(o))return;const s=y({key:o,currentFocusedCell:this.#w,columns:this.#t,rows:this.#o});e.preventDefault(),this.#D(s)}#k(e,t){this.dispatchEvent(new c(e,t))}#F(e){return!e.sortable||this.#s&&this.#s.columnId===e.id?this.#s&&this.#s.columnId===e.id?"ASC"===this.#s.direction?"ascending":"descending":void 0:"none"}#z(e){const t=this.#t.map(((e,t)=>{if(!e.visible)return s.nothing;const o=s.Directives.classMap({firstVisibleColumn:0===t});return s.html`<td aria-hidden="true" class="${o}" data-filler-row-column-index="${t}">`})),o=s.Directives.classMap({"filler-row":!0,"padding-row":!0,"empty-table":0===e});return s.html`<tr aria-hidden="true" class="${o}">${t}`}#A(){this.#d&&(this.#d.documentForCursorChange.body.style.cursor=this.#d.cursorToRestore,this.#d=null,this.#m())}#E(t){if(1!==t.buttons||e.Platform.isMac()&&t.ctrlKey)return;t.preventDefault();const o=t.target;if(!o)return;const s=o.dataset.columnIndex;if(!s)return;const i=globalThis.parseInt(s,10),r=this.#t.findIndex(((e,t)=>t>i&&!0===e.visible)),n=this.#e.querySelector(`td[data-filler-row-column-index="${i}"]`),l=this.#e.querySelector(`td[data-filler-row-column-index="${r}"]`);if(!n||!l)return;const a=this.#e.querySelector(`col[data-col-column-index="${i}"]`),c=this.#e.querySelector(`col[data-col-column-index="${r}"]`);if(!a||!c)return;const d=t.target.ownerDocument;d&&(this.#d={leftCellCol:a,rightCellCol:c,leftCellColInitialPercentageWidth:globalThis.parseInt(a.style.width,10),rightCellColInitialPercentageWidth:globalThis.parseInt(c.style.width,10),initialLeftCellWidth:n.clientWidth,initialRightCellWidth:l.clientWidth,initialMouseX:t.x,documentForCursorChange:d,cursorToRestore:o.style.cursor},d.body.style.cursor="col-resize",o.setPointerCapture(t.pointerId),o.addEventListener("pointermove",this.#g))}#f(e){if(e.preventDefault(),!this.#d)return;const o=this.#d.leftCellColInitialPercentageWidth+this.#d.rightCellColInitialPercentageWidth-10,s=e.x-this.#d.initialMouseX,i=Math.abs(s)/(this.#d.initialLeftCellWidth+this.#d.initialRightCellWidth)*100;let r,n;s>0?(r=t.NumberUtilities.clamp(this.#d.leftCellColInitialPercentageWidth+i,10,o),n=t.NumberUtilities.clamp(this.#d.rightCellColInitialPercentageWidth-i,10,o)):s<0&&(r=t.NumberUtilities.clamp(this.#d.leftCellColInitialPercentageWidth-i,10,o),n=t.NumberUtilities.clamp(this.#d.rightCellColInitialPercentageWidth+i,10,o)),r&&n&&(this.#d.leftCellCol.style.width=r.toFixed(2)+"%",this.#d.rightCellCol.style.width=n.toFixed(2)+"%")}#U(e){e.preventDefault();const t=e.target;t&&(t.releasePointerCapture(e.pointerId),t.removeEventListener("pointermove",this.#g),this.#A())}#L(e,t){const[o]=t;return o!==this.#_()&&e.visible?s.html`<span class="cell-resize-handle" @pointerdown="${this.#E}" @pointerup="${this.#U}" data-column-index="${o}"></span>`:s.nothing}#_(){let e=this.#t.length-1;for(;e>-1;e--){if(this.#t[e].visible)break}return e}#P(e){if(2!==e.button&&-1!==e.button)return;const t=new o.ContextMenu.ContextMenu(e);p(this,t);w(this,t.defaultSection().appendSubMenuItem(F(D.sortBy))),t.defaultSection().appendItem(F(D.resetColumns),(()=>{this.dispatchEvent(new h)})),this.#n&&this.#n.headerRow&&this.#n.headerRow(t,this.#t),t.show()}#N(e){if(2!==e.button&&-1!==e.button)return;if(!(e.target&&e.target instanceof HTMLElement))return;const t=e.target.dataset.rowIndex;if(!t)return;const s=parseInt(t,10),i=this.#o[s-1],r=new o.ContextMenu.ContextMenu(e);w(this,r.defaultSection().appendSubMenuItem(F(D.sortBy)));const n=r.defaultSection().appendSubMenuItem(F(D.headerOptions));p(this,n),n.defaultSection().appendItem(F(D.resetColumns),(()=>{this.dispatchEvent(new h)})),this.#n&&this.#n.bodyRow&&this.#n.bodyRow(r,this.#t,i),r.show()}#W(e){const t=e.target;if(!t)return;const o=Math.round(t.scrollTop+t.clientHeight)===Math.round(t.scrollHeight);this.#r=o?"SCROLLED_TO_BOTTOM":"MANUAL_SCROLL_NOT_BOTTOM",this.#R()}#m(){return H.read((()=>{const e=this.#e.querySelectorAll("th:not(.hidden)"),t=this.#e.querySelectorAll(".cell-resize-handle");this.#e.querySelector("table")&&e.forEach((async(e,o)=>{const s=e.clientWidth,i=e.offsetLeft;if(t[o]){const e=t[o].clientWidth;H.write((()=>{t[o].style.left=i+s-e+"px"}))}}))}))}#G(){return H.read((()=>{const e=this.#e.querySelector(".wrapping-container");let t=0,o=window.innerHeight;e&&(t=e.scrollTop,o=e.clientHeight);const s=18*this.#a;let i=Math.floor((t-s)/18),r=Math.ceil((t+o+s)/18);return i=Math.max(0,i),r=Math.min(this.#o.filter((e=>!e.hidden)).length,r),{topVisibleRow:i,bottomVisibleRow:r}}))}#B(){this.#C=!1}#x(){return this.#w||this.#p}async#R(){if(this.#i)return void(this.#v=!0);this.#i=!0;const{topVisibleRow:e,bottomVisibleRow:t}=await this.#G(),o=this.#o.filter((e=>!e.hidden)),i=o.filter(((o,s)=>s>=e&&s<=t)),r=this.#t.findIndex((e=>e.visible)),n=this.#t.some((e=>!0===e.sortable)),l={"wrapping-container":!0,"show-scrollbar":!0===this.#c};await H.write((()=>{s.render(s.html` ${this.#t.map(((e,t)=>this.#L(e,[t,0])))} <div class="${s.Directives.classMap(l)}" @scroll="${this.#W}" @focusout="${this.#B}"> <table aria-label="${s.Directives.ifDefined(this.#l)}" aria-rowcount="${this.#o.length}" aria-colcount="${this.#t.length}" @keydown="${this.#T}"> <colgroup> ${this.#t.map(((e,t)=>{const o=`width: ${R(this.#t,e.id)}%`;return e.visible?s.html`<col style="${o}" data-col-column-index="${t}">`:s.nothing}))} </colgroup> <thead> <tr @contextmenu="${this.#P}"> ${this.#t.map(((e,t)=>{const o=s.Directives.classMap({hidden:!e.visible,firstVisibleColumn:t===r,sortable:n}),i=this.#x(),l=n&&t===i[0]&&0===i[1];return s.html`<th class="${o}" style="${s.Directives.ifDefined(e.styles?s.Directives.styleMap(e.styles):void 0)}" data-grid-header-cell="${e.id}" @focus="${()=>{this.#D([t,0])}}" @click="${()=>{this.#k(e,t)}}" title="${e.title}" aria-sort="${s.Directives.ifDefined(this.#F(e))}" aria-colindex="${t+1}" data-row-index="0" data-col-index="${t}" tabindex="${s.Directives.ifDefined(n?l?"0":"-1":void 0)}">${e.titleElement||e.title}`}))} </tr> </thead> <tbody> <tr class="filler-row-top padding-row" style="${s.Directives.styleMap({height:18*e+"px"})}" aria-hidden="true"></tr> ${s.Directives.repeat(i,(e=>this.#h.get(e)),(e=>{const t=this.#h.get(e);if(void 0===t)throw new Error("Trying to render a row that has no index in the rowIndexMap");const o=this.#x(),i=t+1,n=!!this.#w&&i===this.#w[1],l=s.Directives.classMap({selected:n,hidden:!0===e.hidden});return s.html` <tr aria-rowindex="${t+1}" class="${l}" style="${s.Directives.ifDefined(e.styles?s.Directives.styleMap(e.styles):void 0)}" @contextmenu="${this.#N}">${this.#t.map(((t,n)=>{const l=S(e,t.id),a=s.Directives.classMap({hidden:!t.visible,firstVisibleColumn:n===r}),c=n===o[0]&&i===o[1],d=t.visible?x(l):null;return s.html`<td class="${a}" style="${s.Directives.ifDefined(t.styles?s.Directives.styleMap(t.styles):void 0)}" tabindex="${c?"0":"-1"}" aria-colindex="${n+1}" title="${l.title||I(String(l.value))}" data-row-index="${i}" data-col-index="${n}" data-grid-value-cell-for-column="${t.id}" @focus="${()=>{this.#D([n,i]),this.dispatchEvent(new m(l,e))}}">${d}`}))} `}))} ${this.#z(i.length)} <tr class="filler-row-bottom padding-row" style="${s.Directives.styleMap({height:18*Math.max(0,o.length-t)+"px"})}" aria-hidden="true"></tr> </tbody> </table> </div> `,this.#e,{host:this})}));const a=this.#x()[1],c=this.#$();this.#C&&a>0&&c&&this.#H(c),this.#M(),this.#I(),this.#b&&this.#m(),this.#i=!1,this.#b=!0,this.#v&&(this.#v=!1,this.#R())}}i.CustomElements.defineComponent("devtools-data-grid",k);var z=Object.freeze({__proto__:null,DataGrid:k});const A=new CSSStyleSheet;A.replaceSync(":host{display:block;height:100%;overflow:hidden}\n/*# sourceURL=dataGridController.css */\n");var E;function U(e){e.style.position="absolute",e.style.left="-999em",e.style.width="100em",e.style.overflow="hidden"}let L,_;!function(e){e.inline="inline",e.list="list",e.both="both",e.none="none"}(E||(E={}));let P=!1;function N(e){(function(){if(!L){const e=document.body.createChild("div");U(e),e.setAttribute("role","alert"),e.setAttribute("aria-atomic","true"),L=e}if(!_){const e=document.body.createChild("div");U(e),e.setAttribute("role","alert"),e.setAttribute("aria-atomic","true"),_=e}return P=!P,P?(_.textContent="",L):(L.textContent="",_)}()).textContent=t.StringUtilities.trimEndWithMaxLength(e,1e4)}const W={sortInAscendingOrder:"{PH1} sorted in ascending order",sortInDescendingOrder:"{PH1} sorted in descending order",sortingCanceled:"{PH1} sorting canceled"},G=l.i18n.registerUIStrings("ui/components/data_grid/DataGridController.ts",W),B=l.i18n.getLocalizedString.bind(void 0,G);class j extends HTMLElement{static litTagName=s.literal`devtools-data-grid-controller`;#e=this.attachShadow({mode:"open"});#b=!1;#t=[];#o=[];#n=void 0;#l=void 0;#c=!1;#j=[];#q=[];#s=null;#V=[];#a;connectedCallback(){this.#e.adoptedStyleSheets=[A]}get data(){return{columns:this.#j,rows:this.#q,filters:this.#V,contextMenus:this.#n,label:this.#l,paddingRowsCount:this.#a,showScrollbar:this.#c}}set data(e){this.#j=e.columns,this.#q=e.rows,this.#n=e.contextMenus,this.#V=e.filters||[],this.#n=e.contextMenus,this.#l=e.label,this.#c=e.showScrollbar,this.#t=[...this.#j],this.#o=this.#K(e.rows,this.#V),!this.#b&&e.initialSort&&(this.#s=e.initialSort),this.#s&&this.#X(this.#s),this.#a=e.paddingRowsCount,this.#R()}#J(e,t,o){let s=!1;const{key:i,text:r,negative:n,regex:l}=t;let a;return a=v(i?[S(e,i)]:e.cells.filter((e=>o.has(e.columnId)))),l?s=l.test(a):r&&(s=a.includes(r.toLowerCase())),n?!s:s}#K(e,t){if(0===t.length)return[...e];const o=new Set(this.#t.filter((e=>e.visible)).map((e=>e.id)));return e.map((e=>{let s=!0;for(const i of t){if(!this.#J(e,i,o)){s=!1;break}}return{...e,hidden:!s}}))}#X(e){const{columnId:t,direction:o}=e;this.#o.sort(((e,s)=>{const i=S(e,t),r=S(s,t),n="number"==typeof i.value?i.value:String(i.value).toUpperCase(),l="number"==typeof r.value?r.value:String(r.value).toUpperCase();return n<l?"ASC"===o?-1:1:n>l?"ASC"===o?1:-1:0})),this.#R()}#k(e){const{column:t}=e.data;t.sortable&&this.#Q(t)}#Q(e){if(this.#s&&this.#s.columnId===e.id){const{columnId:e,direction:t}=this.#s;this.#s="DESC"===t?null:{columnId:e,direction:"DESC"}}else this.#s={columnId:e.id,direction:"ASC"};const t=e.title;this.#s?(this.#X(this.#s),N("ASC"===this.#s.direction?B(W.sortInAscendingOrder,{PH1:t||""}):B(W.sortInDescendingOrder,{PH1:t||""}))):(this.#o=this.#K(this.#q,this.#V),this.#R(),N(B(W.sortingCanceled,{PH1:t||""})))}#Y(e){this.#Q(e.data.column)}#Z(){this.#s=null,this.#o=[...this.#q],this.#R()}#R(){s.render(s.html` <${k.litTagName} .data="${{columns:this.#t,rows:this.#o,activeSort:this.#s,contextMenus:this.#n,label:this.#l,paddingRowsCount:this.#a,showScrollbar:this.#c}}" @columnheaderclick="${this.#k}" @contextmenucolumnsortclick="${this.#Y}" @contextmenuheaderresetclick="${this.#Z}"></${k.litTagName}> `,this.#e,{host:this}),this.#b=!0}}i.CustomElements.defineComponent("devtools-data-grid-controller",j);var q=Object.freeze({__proto__:null,DataGridController:j});class V extends o.Widget.VBox{dataGrid;constructor(e){super(!0,!0),this.dataGrid=new j,this.dataGrid.data=e,this.contentElement.appendChild(this.dataGrid)}data(){return this.dataGrid.data}update(e){this.dataGrid.data=e}}var K=Object.freeze({__proto__:null,DataGridControllerIntegrator:V});export{z as DataGrid,q as DataGridController,K as DataGridControllerIntegrator,g as DataGridEvents,C as DataGridRenderers,$ as DataGridUtils};
